<script type="text/javascript">
    layui.use(['form', 'jquery'], function () {
        $ = layui.jquery;
        var form = layui.form;

        form.verify({
            length: function (value) {
                if (value.length > 10) {
                    return '最多只能10个字';
                }
            }
        });

        form.on('radio(check_product)', function () {
            var xuanzhong = $(this).attr('data-check');
            if (xuanzhong == "false") {
                $($(this).siblings()).each(function (i, input) {
                    $(input).attr('data-check', "false");
                })
                $(this).attr('data-check', "true");
            } else {
                $('ul input').each(function (i, input) {
                    $(input).attr('data-check', "false");
                })
                $(this).prop('checked', false);
                $(this).attr('data-check', "false");
            }
            form.render();
        });

        //获取二级分类
        form.on('select(maintain_father)', function (data) {
            $.ajax({
                url: js_url('shop-get-maintain-list'),
                type: 'get',
                data: {pid: data.value},
                dataType: "json",
                success: function (result) {
                    if (result.state == 0) {
                        //get_goods_list(data.value);
                        var option = '<option value="">请选择二级分类</option>';
                        $.each(result.data, function (index, value) {
                            option += '<option value="' + value.id + '">' + value.name + '</option>';
                        });
                        $('#maintain_son').html(option);
                    }

                    form.render('select');
                }
            });

            /*$(".goods-list").each(function (i) {
                if (data.value) {
                    if (data.value == $(this).attr('maintain-pid')) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                } else {
                    $(this).show();
                }
            });*/

        });

        /*form.on('select(maintain_son)', function (data) {
            $(".goods-list").each(function (i) {
                if (data.value) {
                    if (data.value == $(this).attr('maintain-id')) {
                        $(this).show();
                    } else {
                        $(this).hide();
                    }
                }
            });
        });*/


        form.on('select(product)', function (data) {
            var that = $(this).parent().parent().parent().parent().parent().find('.goods-num');
            var stockArr = JSON.parse($('#stock').val());//转化;
            var stock = stockArr[data.value];
            if (data.value) {
                if (that.val() == 0) {
                    that.val(1);
                }
                if(that.val() > stock){
                    that.val(stock);
                    layer.msg('库存只剩' + stock + '件', {time: 600, icon: 5});
                }
                if (stock < 1) {
                    that.val(0);
                    $(this).parent().parent().parent().find('.pro').val('');
                    form.render('select');
                    layer.msg('库存不足！', {time: 600, icon: 5});
                }

                that.attr('lay-verify', "number|required");
            } else {
                that.val(0);
                that.attr('lay-verify', "number");
            }
        });

        //商品库存判断
        $('.goods-num').bind('input propertychange', function () {
            var stock = JSON.parse($('#stock').val());//转化;
            var that = $(this).parent().parent().prev().find('.pro');
            var key = that.val();
            if (stock[key] < $(this).val()) {
                $(this).val(stock[key]);
                layer.msg('库存只剩' + stock[key] + '件', {time: 600, icon: 5});
            }
            if(!key){
                $(this).val(0);
                layer.msg('请选择规格！', {time: 600, icon: 5});
            }
        })

    });

</script>